package com.qf.anno.web.controller;

import com.qf.anno.domain.Account;
import com.qf.anno.domain.R;
import com.qf.anno.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContextListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //ServletContextListener -- 监听ServletContext对象的创建和销毁的，ServletContext在服务器启动时创建，在服务器关闭时销毁

    @PostMapping
    public R add(@RequestBody Account account){
        accountService.insert(account);
        return R.success("新增成功");
    }

    @PutMapping
    public R update(@RequestBody Account account){
        accountService.update(account);
        return R.success("修改成功");
    }

    @GetMapping
    public R findAll(){
        List<Account> accounts = accountService.findAll();

        return R.success(accounts);
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Integer id){
        accountService.delete(id);
        return R.success("删除成功");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable("id") Integer id){
        Account account = accountService.findById(id);
        return R.success(account);
    }

    @PostMapping("/transfer")
    public R transfer(@RequestBody Map map){
        return accountService.transfer((String) map.get("outName"), (String) map.get("inName"),Double.parseDouble((String) map.get("money")));
    }
}
